package com.xzz.cloud.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.xzz.cloud.alibaba.handler.CustomerBlockHandler;
import com.xzz.cloud.entity.CommonResult;
import com.xzz.cloud.entity.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: hhz
 * @createDate: 2021-4-6
 */
@RestController
public class RateLimitController {
    @GetMapping("/byResource")
    @SentinelResource(value="byResource",blockHandler = "handleException")
    public CommonResult byResource(){
        return new CommonResult(200,"按资源名称限流OK",new Payment());
    }
    public CommonResult handleException(BlockException exception){
        return new CommonResult(404,exception.getClass().getCanonicalName()+"\t 服务不可用");
    }
    @GetMapping("/rateLimit/byUrl")
    @SentinelResource(value="byUrl")
    public CommonResult byUrl(){
        return new CommonResult(200,"按URl限流测试OK",new Payment(2L,"002"));
    }
    @GetMapping("/rateLimit/myBlockHandler")
    @SentinelResource(value="myBlockHandler",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "handlerException2")
    public CommonResult myBlockHandler(){
        return new CommonResult(200,"按自定义流测试OK",new Payment(3L,"003"));
    }
}
